package com.maxd.respository.helpTask;

import com.maxd.model.HelpBrowsingHistory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;


public interface HelpBrowsingHistoryRepository extends Repository<HelpBrowsingHistory, Long> {


    @Query(value = "select t.id,t.title,t.content,t.task_price as taskPrice,t.task_num as taskNum,t.end_num as endNum,t.label,t.open_type as openType," +
            "t.open_content as openContent,t.open_app as openApp,t.verify_content as verifyContent,t.create_time as createTime,t.audit_time as auditTime,t.restrict_time as restrictTime,t.end_time as endTime,t.state" +
            " from help_task t " +
            "left join help_browsing_history bh on bh.help_task_id=t.id " +
            "where bh.user_id=:userId  ",
            countQuery="select count(*) from help_task t " +
                    "left join help_browsing_history bh on bh.help_task_id=t.id " +
                    "where bh.user_id=:userId  ",
            nativeQuery = true)
    Page<Map<String,Object>> selectHelpBrowsingHistoryList(@Param("userId") Long userId, Pageable pageable);


    @Query(value = "select id from help_browsing_history where user_id=:userId and help_task_id=:helpTaskId",nativeQuery = true)
    Long selectHelpBrowsingHistoryByUserId(@Param("userId") Long userId, @Param("helpTaskId") Long helpTaskId);

    @Transactional
    @Modifying
    @Query(value = "update help_browsing_history set create_time=:createTime where id=:helpBrowsingId ",
    nativeQuery = true)
    int updateCreateTimeByUserId(@Param("helpBrowsingId") Long helpBrowsingId, @Param("createTime") Date createTime);


}
